package com.ewanghuiju.app.ui.pintuan.adapter;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.component.ImageLoader;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.bean.response.PintuanShopListResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.util.StringUtil;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author sj  qq:751943509
 * @class 热门的拼团  正在拼的团
 * @time 2020/2/20
 */
public class PintuanGroupListAdapter extends BaseQuickAdapter<PintuanShopListResponBean, PintuanGroupListAdapter.MyBaseViewHolder> {

    HashMap<String, CountDownTimer> timerMap = new HashMap<>();
    ArrayList<String> timerKeyList = new ArrayList<>();
    HashMap<String, Long> secondsMap = new HashMap<>();

    public PintuanGroupListAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(MyBaseViewHolder helper, PintuanShopListResponBean item) {
        TextView tv_pt_title = helper.getView(R.id.tv_shop_title);
        TextView tv_sytime = helper.getView(R.id.tv_sytime);
        StringUtil.getTextStrFormat(tv_pt_title, item.getPeople_number() + "人团", item.getGoods_name());
        helper.setText(R.id.tv_title_icon, item.getPeople_number() + "人团");
        helper.setText(R.id.tv_price, item.getGroup_price());
        helper.setText(R.id.tv_name, TextUtils.isEmpty(item.getNickname()) ? "" : ("发团人：" + item.getNickname()));
        helper.setText(R.id.tv_group_no, TextUtils.isEmpty(item.getGroup_no()) ? "" : ("拼团号：" + item.getGroup_no()));
        helper.setText(R.id.tv_spe_price, item.getResidue_user() + "人");

        RoundedImageView ptImg = helper.getView(R.id.riv_item_icon);
        RoundedImageView headImg = helper.getView(R.id.riv_item_head);

        TextView tv_go_cantuan = helper.getView(R.id.tv_go_cantuan);
        if(item.getUser_yes()==1){
            tv_go_cantuan.setText("已参团");
            tv_go_cantuan.setBackgroundResource(R.drawable.corner_999_50);
        }else{
            tv_go_cantuan.setText("去参团");
            tv_go_cantuan.setBackgroundResource(R.drawable.corner_primary_button_50);
        }
        if (!TextUtils.isEmpty(item.getImage())) {
            ImageLoader.load(mContext, item.getImage(), ptImg);
        }
        ImageLoader.load(mContext, item.getHead_image(), headImg);

        long currenTime = System.currentTimeMillis() / 1000;
        long endTime = item.getEnd_time();
        long seconds = endTime - currenTime;
        String index = String.valueOf(helper.getLayoutPosition());


        if (!timerKeyList.contains(index)) {
            timerKeyList.add(index);
        }

        if (helper.countDownTimer != null) {
            helper.countDownTimer.cancel();
        }

        String thisId = item.getId() + "";
        if (secondsMap.get(thisId) == null) {
            helper.countDownTimer = new CountDownTimer(seconds * 1000, 1000) {
                public void onTick(long l) {
                    tv_sytime.setText("剩余时间：" + StringUtil.getTimeStr(l));
                    secondsMap.put(thisId, l);
                }

                public void onFinish() {
                    //倒计时结束 刷新团信息
                    RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_GROUP_INFO));
                }
            }.start();
        } else {
            helper.countDownTimer = new CountDownTimer(secondsMap.get(thisId), 1000) {
                public void onTick(long l) {
                    tv_sytime.setText("剩余时间：" + StringUtil.getTimeStr(l));
                    secondsMap.put(thisId, l);
                }

                public void onFinish() {
                    //倒计时结束 刷新团信息
                    RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_GROUP_INFO));
                }
            }.start();
        }

        timerMap.put(index, helper.countDownTimer);
    }

    public void onDestroy() {
        for (int i = 0; i < timerKeyList.size(); i++) {
            if (timerKeyList.get(i) != null) {
                if (timerMap.get(timerKeyList.get(i)) != null) {
                    CountDownTimer timer = timerMap.get(timerKeyList.get(i));
                    if (timer != null) {
                        timer.cancel();
                    }
                }
            }
        }
    }

    public class MyBaseViewHolder extends BaseViewHolder {
        private CountDownTimer countDownTimer;

        public MyBaseViewHolder(View view) {
            super(view);
        }
    }
}
